package com.hutao.medical.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.medical.common.util.JsonUtils;
import com.hutao.medical.common.util.StringUtil;
import com.hutao.medical.dataSource.TargetDataSource;
import com.hutao.medical.mapper.dic.DicMapper;
import com.hutao.medical.mapper.retrieval.MedRecordRetrievalMapper;
import com.hutao.medical.pojo.dto.MedRetireDto;
import com.hutao.medical.query.MedicalRetriveVo;
import com.hutao.medical.query.QueryManageVo;
import com.hutao.medical.query.SearchVo;
import com.hutao.medical.service.StatiService;
@Service
public class StatiServiceImpl implements StatiService {

	@Autowired
	private DicMapper dicMapper;
	
	@Autowired
	private MedRecordRetrievalMapper medRecordRetrievalMapper;
	
	@Override
	public String getMedResourceFile(String firstGuid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public List<Map<String, Object>> getDiseaseByCodeOrName(String name) {
		return dicMapper.getDiseaseByCodeOrName(name);
	}

	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public List<Map<String, Object>> getOperationByCodeOrName(String name) {
		return dicMapper.getOperationByCodeOrName(name);
	}

	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public List<String> getClinicalRetrievalList(MedRetireDto medRetireDto) {
		
		return medRecordRetrievalMapper.getClinicalRetrievalList(medRetireDto);
	}

	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public int getClinicalRetrievalCount(MedRetireDto medRetireDto) {
		return medRecordRetrievalMapper.getClinicalRetrievalCount(medRetireDto);
	}

	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public List<Map<String, Object>> getPigeonholeRate(MedRetireDto medRetireDto) {
		return medRecordRetrievalMapper.getPigeonholeRate(medRetireDto);
	}

	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public List<Map<String, Object>> getShelvedRate(MedRetireDto medRetireDto) {
		return medRecordRetrievalMapper.getShelvedRate(medRetireDto);
	}

	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public List<Map<String, Object>> getMedRecordStatisByOffice(MedRetireDto medRetireDto) {
		return medRecordRetrievalMapper.getMedRecordStatisByOffice(medRetireDto);
	}

	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public Map<String, Object> getMedicalRecordRetrieval(MedicalRetriveVo medicalRetriveVo) {
		Map<String,Object> map = new HashMap<>();
		List<Map<String,Object>> medicalRecordRetrievalList =  medRecordRetrievalMapper.getMedicalRecordRetrieval(medicalRetriveVo);
		int totalCount = medRecordRetrievalMapper.getMedicalRecordRetrievalCount(medicalRetriveVo);
		map.put("medicalRecordRetrievalList", medicalRecordRetrievalList);
		map.put("totalCount", totalCount);
		return map;
	}

	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public Map<String, Object> getMedicalRecordRetrievalMap(QueryManageVo queryManageVo) {
		Map<String,Object> map = new HashMap<>();
		
		if(queryManageVo==null){
			return null;
		}
		if(StringUtils.isNotEmpty(queryManageVo.getDiseaseName())){
			List<String> diseaseList = dicMapper.getDiseaseByLikeName(queryManageVo.getDiseaseName());
			if(queryManageVo.getDiseaseType()==1){
				System.out.println(diseaseList);
				if(CollectionUtils.isNotEmpty(diseaseList)){
					for (String disease : diseaseList) {
						queryManageVo.getMainDiseases().add(StringUtil.handleJsonParam(disease));
					}
					//queryManageVo.getMainDiseases().addAll(diseaseList);
				}else{
					queryManageVo.getMainDiseases().add(StringUtil.handleJsonParam(queryManageVo.getDiseaseName()));
				}
			}else{
				if(CollectionUtils.isNotEmpty(diseaseList)){
					
					for (String disease : diseaseList) {
						queryManageVo.getSecondDiseases().add(StringUtil.handleJsonParam(disease));
					}
				}else{
					queryManageVo.getSecondDiseases().add(StringUtil.handleJsonParam(queryManageVo.getDiseaseName()));
				}
			}

			
		}
		/*
		if(StringUtils.isNotEmpty(searchVo.getOperationName())){
			List<String> operationNameList = dicMapper.getOperationByLikeName(searchVo.getOperationName());
				
			if(CollectionUtils.isNotEmpty(operationNameList)){
				searchVo.getOperationNameList().addAll(operationNameList);
			}else{
				searchVo.getOperationNameList().add(searchVo.getOperationName());
			}
			
			
		}*/
		
		List<Map<String,Object>> list = new ArrayList<>();
		List<String> medrecordJsonList = medRecordRetrievalMapper.getMedRecodRetrievalList(queryManageVo);
		if(CollectionUtils.isNotEmpty(medrecordJsonList)){
			for (String medrecordJson : medrecordJsonList) {
				
				list.add(JsonUtils.jsonToPojo(medrecordJson, Map.class));
			}
		}
		int totalCount = medRecordRetrievalMapper.getMedRecodRetrievalCount(queryManageVo);
		map.put("medrecordJsonList", list);
		map.put("totalCount", totalCount);

		return map;
	}

	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public Map<String, Object> getMedicalSpecialRecord(SearchVo searchVo) {
		Map<String,Object> map = new HashMap<>();
		List<Map<String,Object>> specialMedicalList = medRecordRetrievalMapper.getSpecialMedicalList(searchVo);
		int totalCount= medRecordRetrievalMapper.getSpecialMedicalCount(searchVo);
		map.put("specialMedicalList", specialMedicalList);
		map.put("totalCount", totalCount);
		return map;
	}


	@Transactional(readOnly=true)
	@TargetDataSource(name="ds1")
	@Override
	public List<Map<String, Object>> getOfficeWorkInfo(QueryManageVo queryManageVo) {
		List<Map<String, Object>> officeWorkList = medRecordRetrievalMapper.getOfficeWork(queryManageVo);
		if(CollectionUtils.isEmpty(officeWorkList)){
			return null;
		}
		
		for (Map<String, Object> officeWork : officeWorkList) {
			int inHospitalNum =0;
			int rescueNum = 0;
			int rescueSuccessNum=0;
			int specialNum=0;
			int criticalNum=0;
			int sickNum=0;
			if(officeWork.get("outDeptName")==null){
				continue;
			}
			String officeName = (String) officeWork.get("outDeptName");
			
			queryManageVo.setOutDeptName(officeName);
			inHospitalNum = medRecordRetrievalMapper.getInHospitalNum(queryManageVo);
			
			List<Map<String,Object>> nurseDayList = medRecordRetrievalMapper.getNurseDays(queryManageVo);
			if(CollectionUtils.isEmpty(nurseDayList)){
				continue;
			}
			int rescueNumberCount=0;
			int rescueSuccessCount=0;
			int specialNurseNumberCount=0;
			int criticalNumberCount=0;
			int sickNumberCount=0;
			for (Map<String, Object> nurseInfo : nurseDayList) {
				int rescueNumber = (int) nurseInfo.get("rescueNumber");
				int rescueSuccess = (int) nurseInfo.get("rescueSuccess");
				int specialNurseNumber = (int) nurseInfo.get("specialNurseNumber");
				int criticalNumber = (int) nurseInfo.get("criticalNumber");
				int sickNumber = (int) nurseInfo.get("sickNumber");
				if(rescueNumber>0)rescueNumberCount++;
				if(rescueSuccess>0)rescueSuccessCount++;
				if(specialNurseNumber>0)specialNurseNumberCount++;
				if(criticalNumber>0)criticalNumberCount++;
				if(sickNumber>0)sickNumberCount++;
				
			}
			rescueNum+=rescueNumberCount;
			rescueSuccessNum+=rescueSuccessCount;
			specialNum+=specialNurseNumberCount;
			criticalNum+=criticalNumberCount;
			sickNum+=sickNumberCount;
	
			
			officeWork.put("inHospitalNum", inHospitalNum);
			officeWork.put("rescueNum", rescueNum);
			officeWork.put("rescueSuccessNum", rescueSuccessNum);
			officeWork.put("specialNum", specialNum);
			officeWork.put("criticalNum", criticalNum);
			officeWork.put("sickNum", sickNum);
			
			System.out.println(officeWork);
			
		}
		return officeWorkList;
	}

//	@Override
//	public void addMedRecordExamnation(List<MedRecordExamnation> list) {
//		if(CollectionUtils.isNotEmpty(list)){
//			for (MedRecordExamnation medRecordExamnation : list) {
//				medRecordRetrievalMapper.addMedRecordExamnation(medRecordExamnation);
//			}
//		}
//		
//	}


	
	
}
